/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.app;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddMenuReq;
import org.cshoo.tattoo.authorize.api.dto.AddRootMenuReq;
import org.cshoo.tattoo.authorize.api.dto.UpdateMenuReq;
import org.cshoo.tattoo.authorize.domain.po.Menu;
import org.cshoo.tattoo.authorize.domain.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
@Slf4j
public class MenuAppService {

    @Resource
    private MenuService menuService;

    /**
     * 判断菜单编号是否存在
     *
     * @return 判断结果
     */
    public boolean menuCodeExists(String code, Long appId) {
        return menuService.menuCodeExists(code, appId);
    }

    /**
     * 添加根菜单
     *
     * @param addRootMenuReq 添加根菜单请求
     */
    public void addRootMenu(AddRootMenuReq addRootMenuReq) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(addRootMenuReq, menu);
        menuService.addRootMenu(menu);
    }

    /**
     * 添加菜单
     *
     * @param addMenuReq 添加菜单请求
     */
    public void addMenu(AddMenuReq addMenuReq) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(addMenuReq, menu);
        menuService.addMenu(menu);
    }

    /**
     * 修改菜单数据，这里只修改uri，name，description属性
     *
     * @param updateMenuReq 修改菜单请求
     */
    public void updateMenu(UpdateMenuReq updateMenuReq) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(updateMenuReq, menu);
        menuService.updateMenu(menu);
    }

    /**
     * 删除菜单
     *
     * @param id id
     */
    public void deleteMenu(Long id) {
        menuService.deleteMenu(id);
    }

    /**
     * 根据appId查询根菜单列表
     *
     * @param appId app id
     * @return 根菜单列表
     */
    public List<Menu> findRootMenuByApp(Long appId) {
        return menuService.findRootMenuByApp(appId);
    }

    /**
     * 根据appId查询菜单列表
     *
     * @param appId app id
     * @return 菜单列表
     */
    public List<Menu> findMenuByApp(Long appId) {
        return menuService.findMenuByApp(appId);
    }

    /**
     * 根据父级菜单id查询子级菜单
     *
     * @param parentId 父级别菜单id
     * @return 子级菜单
     */
    public List<Menu> findMenusByParentId(Long parentId) {
        return menuService.findMenusByParentId(parentId);
    }

    /**
     * 根据角色id查询菜单,即查询这个角色有哪些菜单权限
     *
     * @param roleId 角色id
     * @return 菜单
     */
    public List<Menu> findByRoleId(Long roleId) {
        return menuService.findByRoleId(roleId);
    }

}
